package com.sh.shanhuo.activity;


import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sh.shanhuo.R;
import com.sh.shanhuo.bean.AppVersionBean;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.utils.Tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class VersionActivity extends BaseActivity {

    private String url;
    private TextView version;
    private TextView newVersion;
    private LinearLayout bottomLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_version;
    }

    @Override
    protected void initView() {
        super.initView();
        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> finish());
        bottomLayout = findViewById(R.id.bottomLayout);
        version = findViewById(R.id.version);
        TextView update = findViewById(R.id.update);
        newVersion = findViewById(R.id.newVersion);
        update.setOnClickListener(v -> {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        getNewVersion();
        getCurVersion();
    }

    private void getNewVersion() {
        OkHttpUtil.getInstance().postString(OkHttpUtil.IP + OkHttpUtil.NEW_VERSION, "", new GsonObjectCallback<AppVersionBean>() {
            @Override
            public void onUi(AppVersionBean appVersionBean) {
                if (appVersionBean.getData() != null) {
                    url = appVersionBean.getData().getAppUrl();

                    String appVersion = appVersionBean.getData().getAppVersion();
                    String currentVersion = Tools.getAppVersionName(VersionActivity.this);
                    String regEx = "[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m0 = p.matcher(appVersion);
                    Matcher m1 = p.matcher(currentVersion);

                    if (Integer.parseInt(m0.replaceAll("").trim()) <=
                            Integer.parseInt(m1.replaceAll("").trim())) {
                        bottomLayout.setVisibility(View.GONE);
                    } else {
                        bottomLayout.setVisibility(View.VISIBLE);
                    }
                    newVersion.setText(new StringBuilder().append("最新版本 ").append(appVersionBean.getData().getAppVersion()));
                }
            }

            @Override
            public void onFailed(Call call, IOException e) {
                version.setText(Tools.getAppVersionName(VersionActivity.this));
            }
        });
    }

    private void getCurVersion() {
        String curVer = Tools.getAppVersionName(this);
        Map<String, String> params = new HashMap<>();
        params.put("appVersion", curVer);
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.CUR_VERSION, params, new GsonObjectCallback<AppVersionBean>() {
            @Override
            public void onUi(AppVersionBean appVersionBean) {
                if (appVersionBean.getData() != null) {
                    version.setText(new StringBuilder()
                            .append("当前版本：").append(curVer)
                            .append("\n版本说明：").append(appVersionBean.getData().getIntroduce()));
                } else {
                    version.setText(Tools.getAppVersionName(VersionActivity.this));
                }
            }

            @Override
            public void onFailed(Call call, IOException e) {
                version.setText(Tools.getAppVersionName(VersionActivity.this));
            }
        });
    }
}
